$().ready(function() {
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
$(function(){
	$('.summernote').summernote({
		height: 500,
		width: 750,
		tabsize: 2,
		lang: 'zh-CN'
	});
})
function save() {
	var content_sn = $("#content_sn").summernote('code');
	$("#content").val(content_sn);
	$.ajax({
		cache : true,
		type : "POST",
		url : "/system/dlNews/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("服务器错误");
		},
		success : function(data) {
			if(data.code == 0){
				parent.layer.alert(data.msg)
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
			}else{
				parent.layer.alert(data.msg)
			}

		}
	});

}
layui.use(['form', 'layedit', 'laydate'], function(){
	var form = layui.form
		,layer = layui.layer
		,layedit = layui.layedit
		,laydate = layui.laydate;

	//自定义验证规则
	form.verify({
		otherReq: function(value,item){
			var $ = layui.$;
			var verifyName=$(item).attr('name')
				, verifyType=$(item).attr('type')
				,formElem=$(item).parents('.layui-form')//获取当前所在的form元素，如果存在的话
				,verifyElem=formElem.find('input[name='+verifyName+']')//获取需要校验的元素
				,isTrue= verifyElem.is(':checked')//是否命中校验
				,focusElem = verifyElem.next().find('i.layui-icon');//焦点元素
			if(!isTrue || !value){
				//定位焦点
				focusElem.css(verifyType=='radio'?{"color":"#FF5722"}:{"border-color":"#FF5722"});
				//对非输入框设置焦点
				focusElem.first().attr("tabIndex","1").css("outline","0").blur(function() {
					focusElem.css(verifyType=='radio'?{"color":""}:{"border-color":""});
				}).focus();
				return '必填项不能为空';
			}
		}
	});
});
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			}
		}
	})
}
function fileUpload(id, type){
	var photo=$("#"+id).val();
	if(photo!=null && photo!=""){
		// 构建数据
		var data = new FormData()
		data.append('type', type)
		data.append('name', $('#'+id).val())
		data.append('file', $('#'+id)[0].files[0]) // file 对象
		$.ajax({
			cache : false,
			type : "POST",
			url : "/upload",
			data :data,
			processData: false,//数据不被转换为字符串
			contentType: false,//上传文件时使用，避免 JQuery 对其操作
			async : false,
			dataType:"json",
			error : function(request) {
			},
			success : function(data) {
				if (data.code == 0) {
					photo = data.path;
					$("input[name='"+id+"']").val(data.newFileName);
					console.log(photo);
					$("#show"+id).attr("src",photo);
				}
			}
		});
	}

}